package com.ch.base.utils;

import com.ch.base.enums.OrderExcelAEnum;
import com.ch.base.exception.HandlerException;
import com.ch.base.model.ChBasicOrderModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * process order util
 * created in 2019-04-19 13:47
 *
 * @author ray
 * @version 1.0
 **/
@Slf4j
public class OrderUtil {

    public static void check(List<Map<String, String>> data) {

        log.info("start:check order excel title");
        Map<String, String> head = data.get(0);
        Arrays.stream(OrderExcelAEnum.values()).forEach(title -> {
            if (StringUtils.isEmpty(head.get(title.toString()))
                    || !StringUtils.equals(head.get(title.toString()).trim(), title.getTitleName())) {
                log.error("第[" + title.toString() + "]列标题格式错误");
                throw new HandlerException("第[" + title.toString() + "]列标题格式错误");
            }
        });
        data.subList(0, 1).clear();
        if (CollectionUtils.isEmpty(data)) {
            log.error("order excel无数据");
            throw new HandlerException("order excel无数据");
        }
        log.info("end:check order excel title");
    }

    public static List<ChBasicOrderModel> copyProperties(List<Map<String, String>> data) {

        List<ChBasicOrderModel> orders = new ArrayList<>();
        data.forEach(datum -> {
            ChBasicOrderModel order = new ChBasicOrderModel();
            order.setOrderId(StringUtils.trim(datum.get("A")));
            try {
                order.setOrderTime(Constant.STF.format(Constant.STF.parse(datum.get("B"))));
            } catch (ParseException e) {
                throw new HandlerException("excel时间格式错误");
            }
            order.setReceiverName(StringUtils.trim(datum.get("C")));
            order.setReceiverPhone(StringUtils.trim(datum.get("D")));
            order.setReceiverAddress(StringUtils.trim(datum.get("E")));
            order.setProvince(StringUtils.trim(datum.get("F")));
            order.setCity(StringUtils.trim(datum.get("G")));
            order.setDistrict(StringUtils.trim(datum.get("H")));
            order.setItemName(StringUtils.trim(datum.get("I")));
            order.setPurchaseNum(Integer.valueOf(datum.get("J")));
            order.setOrderMoney(BigDecimal.valueOf(Double.parseDouble(datum.get("K"))));
            order.setItemSpecification(StringUtils.trim(datum.get("L")));
            order.setOrderNote(StringUtils.trim(datum.get("M")));
            order.setOrderStatus(StringUtils.trim(datum.get("N")));
            orders.add(order);
        });
        return orders;
    }
}
